package com.pe1n.yangtaopai.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jude.rollviewpager.RollPagerView;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.bean.MultiTitleBean;
import com.pe1n.yangtaopai.bean.OtherBean;
import com.pe1n.yangtaopai.bean.PreferenceBean;
import com.pe1n.yangtaopai.bean.RecommendBean;
import com.pe1n.yangtaopai.bean.SpecialBean;
import com.pe1n.yangtaopai.bean.TypeBean;
import com.pe1n.yangtaopai.view.activity.ItemOneListActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Pe1n on 2016/11/23.
 */

public class MyBaihuoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<Object> mList;
    private List<OtherBean.DataBean> mOtherList;
    private final int TYPE1 = 0, TYPE2 = 1, TYPE3 = 2, TYPE4 = 3, TYPE5 = 4, TYPE6 = 5, TYPE7 = 6;

    public MyBaihuoAdapter(Context mContext, List<Object> mList, List<OtherBean.DataBean> mOtherList) {
        this.mContext = mContext;
        this.mList = mList;
        this.mOtherList = mOtherList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {

            case 0:
                View view1 = LayoutInflater.from(mContext).inflate(R.layout.item_single_title_layout, parent, false);
                return new MyViewHolder1(view1);

            case 1:
                View view2 = LayoutInflater.from(mContext).inflate(R.layout.item_type_layout, parent, false);
                return new MyViewHolder2(view2);

            case 2:
                View view3 = LayoutInflater.from(mContext).inflate(R.layout.item_preference_layout, parent, false);
                return new MyViewHolder3(view3);

            case 3:
                View view4 = LayoutInflater.from(mContext).inflate(R.layout.item_special_layout, parent, false);
                return new MyViewHolder4(view4);

            case 4:
                View view5 = LayoutInflater.from(mContext).inflate(R.layout.item_recommend_layout, parent, false);
                return new MyViewHolder5(view5);

            case 5:
                View view6 = LayoutInflater.from(mContext).inflate(R.layout.item_other_layout, parent, false);
                return new MyViewHolder6(view6);

            case 6:
                View view7 = LayoutInflater.from(mContext).inflate(R.layout.item_multi_title_layout, parent, false);
                return new MyViewHolder7(view7);
        }

        return null;
    }

    @Override
    public int getItemViewType(int position) {

        switch (position) {

            case 0:
                MultiTitleBean bean = (MultiTitleBean) mList.get(position);
                if (bean.getData().size() > 1) {
                    return TYPE7;
                }
                return TYPE1;

            case 1:
                return TYPE2;

            case 2:
                return TYPE3;

            case 3:
                return TYPE4;

            case 4:
                return TYPE5;

            case 5:
                return TYPE6;

            case 6:
                return TYPE7;

        }

        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof MyViewHolder1) {

            List<MultiTitleBean.DataBean> mSingleTitleBeanList = ((MultiTitleBean) mList.get(position)).getData();
            String imgUrl = mSingleTitleBeanList.get(0).getImagePath();
            Glide.with(mContext).load(imgUrl).placeholder(R.drawable.place_holder).into(((MyViewHolder1) holder).imageView);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent  = new Intent(mContext, ItemOneListActivity.class);
                    intent.putExtra("OneImgTitle",((MultiTitleBean) mList.get(position)).getData().get(0).getTitle());
                    intent.putExtra("ID",((MultiTitleBean) mList.get(position)).getData().get(0).getTargetId());
                    mContext.startActivity(intent);
                }
            });

        } else if (holder instanceof MyViewHolder2) {

            List<TypeBean.DataBean> mTypeBeanList = ((TypeBean) mList.get(position)).getData();
            ((MyViewHolder2) holder).setData(mTypeBeanList);

        } else if (holder instanceof MyViewHolder3) {

            final List<PreferenceBean.DataBean> mPreferenceBeanList = ((PreferenceBean) mList.get(position)).getData();
            for (int i = 0; i < mPreferenceBeanList.size(); i++) {
                String imgUrl = mPreferenceBeanList.get(i).getImagePath();
                switch (i) {

                    case 0:
                        Glide.with(mContext).load(imgUrl).placeholder(R.drawable.place_holder).into(((MyViewHolder3) holder).imageViewLeft);
                        break;

                    case 1:
                        Glide.with(mContext).load(imgUrl).placeholder(R.drawable.place_holder).into(((MyViewHolder3) holder).imageViewRightTop);
                        break;

                    case 2:
                        Glide.with(mContext).load(imgUrl).placeholder(R.drawable.place_holder).into(((MyViewHolder3) holder).imageViewRightBottom);
                        break;

                }
            }

            ((MyViewHolder3) holder).imageViewLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent  = new Intent(mContext, ItemOneListActivity.class);
                    intent.putExtra("OneImgTitle",mPreferenceBeanList.get(0).getTitle());
                    intent.putExtra("ID",mPreferenceBeanList.get(0).getTargetId());
                    mContext.startActivity(intent);
                }
            });

            ((MyViewHolder3) holder).imageViewRightTop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent  = new Intent(mContext, ItemOneListActivity.class);
                    intent.putExtra("OneImgTitle",mPreferenceBeanList.get(1).getTitle());
                    intent.putExtra("ID",mPreferenceBeanList.get(1).getTargetId());
                    mContext.startActivity(intent);
                }
            });

            ((MyViewHolder3) holder).imageViewRightBottom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent  = new Intent(mContext, ItemOneListActivity.class);
                    intent.putExtra("OneImgTitle",mPreferenceBeanList.get(2).getTitle());
                    intent.putExtra("ID",mPreferenceBeanList.get(2).getTargetId());
                    mContext.startActivity(intent);
                }
            });

        } else if (holder instanceof MyViewHolder4) {

            List<SpecialBean.DataBean> mSpecialBeanList = ((SpecialBean) mList.get(position)).getData();
            ((MyViewHolder4) holder).setData(mSpecialBeanList);

        } else if (holder instanceof MyViewHolder5) {

            List<RecommendBean.DataBean> mRecommendBeanList = ((RecommendBean) mList.get(position)).getData();
            ((MyViewHolder5) holder).setData(mRecommendBeanList);

        } else if (holder instanceof MyViewHolder6) {

            //List<OtherBean.DataBean> mOtherBeanList = ((OtherBean) mList.get(position)).getData();
            ((MyViewHolder6) holder).setData(mOtherList);

        } else if (holder instanceof MyViewHolder7) {

            List<MultiTitleBean.DataBean> mMultiTitleBeanList = ((MultiTitleBean) mList.get(position)).getData();
            ((MyViewHolder7) holder).setData(mMultiTitleBeanList);

        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder1 extends RecyclerView.ViewHolder {

        private ImageView imageView;

        public MyViewHolder1(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.iv_single_title);
        }

    }

    class MyViewHolder2 extends RecyclerView.ViewHolder {

        private RecyclerView recyclerView;
        private List<TypeBean.DataBean> mTypeBeanList = new ArrayList<>();

        public MyViewHolder2(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recycler_view_type);
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4, OrientationHelper.VERTICAL, false));
            recyclerView.setAdapter(new MyTypeAdapter(mContext, mTypeBeanList));
        }

        public void setData(List<TypeBean.DataBean> newList) {
            mTypeBeanList.clear();
            mTypeBeanList.addAll(newList);
            recyclerView.getAdapter().notifyDataSetChanged();
        }

    }

    class MyViewHolder3 extends RecyclerView.ViewHolder {

        private ImageView imageViewLeft, imageViewRightTop, imageViewRightBottom;

        public MyViewHolder3(View itemView) {
            super(itemView);
            imageViewLeft = (ImageView) itemView.findViewById(R.id.iv_left_preference);
            imageViewRightTop = (ImageView) itemView.findViewById(R.id.iv_right_top_preference);
            imageViewRightBottom = (ImageView) itemView.findViewById(R.id.iv_right_bottom_preference);
        }

    }

    class MyViewHolder4 extends RecyclerView.ViewHolder {

        private RecyclerView recyclerView;
        private List<SpecialBean.DataBean> mSpecialBeanList = new ArrayList<>();

        public MyViewHolder4(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recycler_view_special);
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 2, OrientationHelper.VERTICAL, false));
            recyclerView.setAdapter(new MySpecialAdapter(mContext, mSpecialBeanList));
        }

        public void setData(List<SpecialBean.DataBean> newList) {
            mSpecialBeanList.clear();
            mSpecialBeanList.addAll(newList);
            recyclerView.getAdapter().notifyDataSetChanged();
        }

    }

    class MyViewHolder5 extends RecyclerView.ViewHolder {

        private RecyclerView recyclerView;
        private List<RecommendBean.DataBean> mRecommendBeanList = new ArrayList<>();

        public MyViewHolder5(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recycler_view_recommend);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext, OrientationHelper.VERTICAL, false));
            recyclerView.setAdapter(new MyRecommendAdapter(mContext, mRecommendBeanList));
        }

        public void setData(List<RecommendBean.DataBean> newList) {
            mRecommendBeanList.clear();
            mRecommendBeanList.addAll(newList);
            recyclerView.getAdapter().notifyDataSetChanged();
        }

    }

    class MyViewHolder6 extends RecyclerView.ViewHolder {

        private RecyclerView recyclerView;
        private List<OtherBean.DataBean> mOtherBeanList = new ArrayList<>();

        public MyViewHolder6(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recycler_view_other);
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 2, OrientationHelper.VERTICAL, false));
            recyclerView.setAdapter(new MyOtherAdapter(mContext, mOtherBeanList));
        }

        public void setData(List<OtherBean.DataBean> newList) {
            mOtherBeanList.clear();
            mOtherBeanList.addAll(newList);
            recyclerView.getAdapter().notifyDataSetChanged();
        }

    }

    class MyViewHolder7 extends RecyclerView.ViewHolder {

        private RollPagerView rollPagerView;
        private List<MultiTitleBean.DataBean> mMultiTitleBeanList = new ArrayList<>();

        public MyViewHolder7(View itemView) {
            super(itemView);
            rollPagerView = (RollPagerView) itemView.findViewById(R.id.rpv_multi_title);
            rollPagerView.setAdapter(new MyMultiTitleAdapter(mContext, mMultiTitleBeanList, rollPagerView));
        }

        public void setData(List<MultiTitleBean.DataBean> newList) {
            mMultiTitleBeanList.clear();
            mMultiTitleBeanList.addAll(newList);
            rollPagerView.getViewPager().getAdapter().notifyDataSetChanged();
        }

    }

}
